package com.kuqi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.kuqi.adapter.BookInfoTabAdapter;
import com.kuqi.common.APPCONST;
import com.kuqi.core.AppActivity;
import com.kuqi.entitiy.Book;
import com.kuqi.fragments.BookInfoAuthorFragment;
import com.kuqi.fragments.BookInfoDetailFragment;
import com.kuqi.utils.UrlTool;
import java.util.ArrayList;

public class BookInfoActivity extends AppActivity {

    // 当前书籍的实体
    private Book bookInfo;

    // 界面控件对象
    ArrayList<Fragment> fragments = new ArrayList<>();

    // 与界面控件一一对应的名称
    ArrayList<String> fragmentTabNames = new ArrayList<>();

    // 界面控件
    private ImageView iv_book_cover;
    private TextView tv_book_name;
    private TextView tv_book_author;
    private TextView tv_prefer_num;
    private TextView tv_book_price;
    private ImageButton ib_read_online;
    private ImageButton ib_buy_now;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.book_info_page);

        // 获取页面传递来的参数
        Intent intent = this.getIntent();
        bookInfo = (Book)intent.getSerializableExtra("book_info");

        // 获取控件
        iv_book_cover = findViewById(R.id.iv_book_cover);
        tv_book_name = findViewById(R.id.tv_book_name);
        tv_book_author = findViewById(R.id.tv_book_author);
        tv_prefer_num = findViewById(R.id.tv_prefer_num);
        tv_book_price = findViewById(R.id.tv_book_price);
        ib_read_online = findViewById(R.id.ib_read_online);
        ib_buy_now = findViewById(R.id.ib_buy_now);

        // 隐藏标题栏
        hideActionBar();

        // 初始化页面显示内容
        initTabComp();

        // 填充内容
        fillInformation();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }


    /**
     * 初始化Tab选择显示内容
     */
    private void initTabComp(){
        BookInfoAuthorFragment authorFragment= new BookInfoAuthorFragment(bookInfo);
        BookInfoDetailFragment detailFragment = new BookInfoDetailFragment(bookInfo);
        fragments.add(authorFragment);
        fragments.add(detailFragment);
        fragmentTabNames.add("作者信息");
        fragmentTabNames.add("书籍简介");

        BookInfoTabAdapter pagerAdapter = new BookInfoTabAdapter(getSupportFragmentManager(), fragments, fragmentTabNames);
        ViewPager viewPager = findViewById(R.id.vp_info_shown);
        viewPager.setAdapter(pagerAdapter);
        TabLayout tabLayout = findViewById(R.id.tl_info_tab);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }


    /**
     * 填充信息到对应的位置
     */
    private void fillInformation(){
        if(bookInfo == null){
            Toast.makeText(this, "没有获取的数据信息!异常!",Toast.LENGTH_SHORT).show();
        }else{
            Glide.with(this)
                    .load(UrlTool.book_base + bookInfo.getThumbnail())
                    .into(iv_book_cover);
            tv_book_name.setText(bookInfo.getBookName());
            tv_book_author.setText(bookInfo.getAuthor());
            // 订阅和价格暂时没有
            ib_read_online.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Toast.makeText(getApplicationContext(),"点击在线阅读！.", Toast.LENGTH_SHORT).show();
                    Intent intent= new Intent(BookInfoActivity.this, ReadActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(APPCONST.BOOK, bookInfo);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
            ib_buy_now.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(),"小爷看书不花钱，在线阅读看个完！.", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

}
